<template>
	<div>
		<view class="xc-wrap xp-wrap">
			<!-- #ifdef MP-WEIXIN -->
			<uv-navbar v-if="sr.paper" :title="sr.paper.themetopnavigationtitle" placeholder border></uv-navbar>
			<!-- #endif -->

			<xa-paper-none v-if="control.currentpage=='none'"></xa-paper-none>
			<xa-paper-displayOuttime v-if="control.currentpage=='displayOuttime'" :data="sr"></xa-paper-displayOuttime>
			<xa-paper-displayPaper v-if="control.currentpage=='displayPaper'" :data="sr"
				@reset_parent="public_reset"></xa-paper-displayPaper>
			<xa-paper-displayPageResult v-if="control.currentpage=='displayPageResult'" :data="sr"
				@reset_parent="public_reset"></xa-paper-displayPageResult>
		</view>
		<xc-uv-clogin-loginform ref="xc-uv-clogin-loginform" @onLoginSuccess="onLoginSuccess"
			:can-close="false"></xc-uv-clogin-loginform>
	</div>
</template>

<script>
	import config from "@/config/config.js";
	import xcUniappUtils from '@/library/xcore/js/XcUniappUtils.js';
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import xcLangUtils from '@/library/xcore/js/XcLangUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';


	export default {
		data() {
			return {
				config: config,
				xsStringUtils: xsStringUtils,
				xsDateUtils: xsDateUtils,
				sp: {
					id: null,
					locales: null,
				},
				sr: {
					paper: null,
					questionjson: [],
					usercount: 0,
					doing_userpaper: null,
					hasFinishAnswer: null,
				},
				control: {
					// none: 没有工单
					// displayOuttime: 工单还没有开始或已经结束
					// displayPaper:显示工单待用户回答
					// displayPageResult: 已经提交工单
					currentpage: null,
				}
			}
		},
		onLoad(options) {
			let that = this;
			console.log("options=",options);
			let sp_id = options.id ? options.id : xcHttpUtils.getUrlParam(options,"id");
			that.sp.id = parseInt(sp_id);
			//xcLangUtils.switchLocale((options.locales != null && options.locales != '') ? options.locales : "zh")

			that.xinit();

			xcUniappUtils.onShow(function() {
				that.public_show();
			});
		},
		onShow() {
			let that = this;

		},
		// onPullDownRefresh() {
		// 	let that = this;
		// 	setTimeout(function() {
		// 		that.public_show();
		// 		uni.stopPullDownRefresh();
		// 	}, 1000);
		// },
		methods: {
			xinit() {
				let that = this;
				if (window) {
					window.themepreview = this.themepreview;
				}
			},
			public_reset() {
				let that = this;
				console.log("reset paper view");
				that.public_show();
			},
			public_show() {
				let that = this;
				console.log("paper show");
				xcHttpUtils.get('csmwj/xpmy_api/paper', {
					'id': that.sp.id
				}, function(res) {
					if (res.userid == null) {
						that.$refs["xc-uv-clogin-loginform"].public_open();
					}
					that.sr = {
						paper: res.paper,
						questionjson: res.questionjson,
						usercount: res.usercount,
						doing_userpaper: res.doing_userpaper,
						hasFinishAnswer: res.hasFinishAnswer,
					}

					xcLangUtils.switchLocale((that.sr.paper.locales == 'en') ? 'en' : 'zh');

					uni.setNavigationBarTitle({
						title: xsStringUtils.isEmpty(that.sr.paper.themetopnavigationtitle)?that.sr.paper.name:that.sr.paper.themetopnavigationtitle
					});

					if (that._isOutintime() === true) {
						that.control.currentpage = 'displayOuttime';
					} else {
						if (that.sr.hasFinishAnswer == 'Y') {
							that.control.currentpage = 'displayPageResult';
						} else {
							that.control.currentpage = 'displayPaper';
						}
					}
					// copy from xa-paper-displayPaper-body

					if (that.sr.doing_userpaper == null) {
						xcHttpUtils.post('csmwj/xpmy_api/createuserpage', {
							'id': that.sr.paper.id,
						}, function(res2) {
							that.sr = {
								paper: that.sr.paper,
								questionjson: that.sr.questionjson,
								usercount: that.sr.usercount,
								doing_userpaper: res2.userpaper,
								hasFinishAnswer: that.sr.hasFinishAnswer,
							}

						})
					} else {
						xcViewUtils.toast(that.$t("xp.paper.msg_continue_last_answer"));
					}
				}, function(msg) {
					that.control.currentpage = 'none';
				});
			},
			_isOutintime() {
				let that = this;
				let now = new Date().getTime() / 1000;

				if (that.sr.paper.cfghasdatelimit == 'Y') {
					if (now < that.sr.paper.cfgbegintime || now > that.sr.paper.cfgendtime) {
						return true;
					} else {
						return false;
					}
				}
				return false;
			},
			onLoginSuccess(userinfo) {
				let that = this;
				that.sr.userid = userinfo.id;
				that.public_reset();
			},

		},
	}
</script>

<style lang="scss">
	.xp-wrap {
		padding-top: 20px;
		max-width: 750px;
		margin: auto;
	}
</style>
